package cn.spike.org.service.impl;

import cn.spike.basic.exception.BusinessException;
import cn.spike.basic.service.impl.BaseServiceImpl;
import cn.spike.basic.util.BaiduAiUtils;
import cn.spike.basic.util.Md5Utils;
import cn.spike.basic.util.StrUtils;
import cn.spike.org.domain.Employee;
import cn.spike.org.domain.Shop;
import cn.spike.org.domain.ShopAuditLog;
import cn.spike.org.dto.ShopDto;
import cn.spike.org.mapper.EmployeeMapper;
import cn.spike.org.mapper.ShopAuditLogMapper;
import cn.spike.org.mapper.ShopMapper;
import cn.spike.org.service.ShopService;
import cn.spike.user.domain.Logininfo;
import cn.spike.user.mapper.LogininfoMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
@Transactional
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements ShopService {

    @Autowired
    private LogininfoMapper logininfoMapper;

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;

    @Autowired
    private JavaMailSender javaMailSender;

    @Override
    @Transactional
    public void settlement(Shop shop) {
        //一：检验 - 空值校验：前端的js校验容易被禁用，不安全，后端也要做校验
        if(StringUtils.isEmpty(shop.getName())){ //空和空字符串返回true
            throw new BusinessException("提交资料不能为空!!!");
        }

        //校验店铺名称
        if(!BaiduAiUtils.TextCensor(shop.getName())){
            throw new BusinessException("店铺名称不合法!!!");
        }
        //校验logo
        if(!StringUtils.isEmpty(shop.getLogo())){
            if(!BaiduAiUtils.ImgCensor("http://123.207.27.208"+shop.getLogo())){
                throw new BusinessException("logo不合法!!!");
            }
        }

        //二：校验 - 当前店铺是否入驻过
        Shop dbShop = shopMapper.findByNameAndAddress(shop);
        if(dbShop != null){//查到了 = 入驻过
            throw new BusinessException("请勿重复入驻，忘记密码请联系管理员!!!");
        }

        //添加logininfo信息
        Employee admin = shop.getAdmin();
        //处理盐值和密码
        String salt = StrUtils.getComplexRandomString(32);
        String md5Pwd = Md5Utils.encrypByMd5(admin.getPassword() + salt);
        admin.setSalt(salt);
        admin.setPassword(md5Pwd);

        //添加logininfo信息
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(admin,logininfo);
        logininfo.setType(0);//管理员
        logininfo.setDisable(true);//可以登陆
        logininfoMapper.add(logininfo);

        //三：数据库操作
        //1.保存t_employee员工信息 - 生成自增id
        admin.setLogininfo_id(logininfo.getId());
        employeeMapper.add(admin);//只要添加之后，就生成了自增id

        //2.将自增id设置到shop对象，并保存店铺信息 - 生成店铺的自增id
        shop.setAdminId(admin.getId());
        shopMapper.add(shop);

        //3.将店铺的shopid设置到employee对象，修改employee信息
        admin.setShop_id(shop.getId()); //admin对象：id，shopId
        employeeMapper.update(admin);
    }

    @Override
    public void auditPass(ShopAuditLog log) throws MessagingException {
        //1.修改店铺状态
        shopMapper.updateState(2,log.getShopId());
        //2.添加审核日志
        log.setState(1);
        log.setAuditId(350L);
        shopAuditLogMapper.add(log);
        //3.发送激活邮件
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //发送复杂邮件的工具类
        //true - 能够添加附件
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true,"utf-8");
        helper.setFrom("794921047@qq.com");//设置发件人
        helper.setSubject("宠物之家店铺激活邮件");//设置邮件主题

        Shop shop = shopMapper.loadById(log.getShopId());
        Employee admin = employeeMapper.loadById(shop.getAdminId());

        //true - 能够再内容中编写html标签 - 会解析
        helper.setText("<h1>你的店铺已经注册!!!</h1><img src='http://123.207.27.208"+shop.getLogo()+"' >" +
                "<a href='http://127.0.0.1:8080/shop/active/"+shop.getId()+"'>点击该链接激活</a>",true);
        //设置收件人
        helper.setTo(admin.getEmail());
        //发送邮件
        javaMailSender.send(mimeMessage);
    }

    @Override
    public void auditReject(ShopAuditLog log) throws MessagingException {
        //1.修改店铺状态
        shopMapper.updateState(4,log.getShopId());
        //2.添加审核日志
        log.setState(0);
        log.setAuditId(350L);
        shopAuditLogMapper.add(log);
        //3.发送邮件给店铺管理员 - 告知入驻失败修改入驻信息重新申请
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //发送复杂邮件的工具类
        //true - 能够添加附件
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true,"utf-8");
        helper.setFrom("794921047@qq.com");//设置发件人
        helper.setSubject("宠物之家店铺入驻审核失败");
        Shop shop = shopMapper.loadById(log.getShopId());
        Employee admin = employeeMapper.loadById(shop.getAdminId());
        //true - 能够再内容中编写html标签 - 会解析
        helper.setText("<h1>店铺注册信息有不实信息</h1>" +
                "<a href='http://127.0.0.1:8081/#/register'>点击重新填写入驻信息</a>",true);
        //收件人
        helper.setTo(admin.getEmail());
        javaMailSender.send(mimeMessage);
    }

    @Override
    public void activation(Long id) {
        shopMapper.updateState(3,id);
    }

    @Override
    public List<ShopDto> echarts() {
        return shopMapper.echarts();
    }


}
